/**
 * @file   GetClientStatusResponsePacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-05-13
 * @brief  DataBus Get Client Status Request packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/GetClientStatusResponsePacket.h>

bool DataBus::GetClientStatusResponsePacket::create(const quint8 destination,
                                                    const quint8 packetId,
                                                    const quint8 clientId,
                                                    const ClientStatus clientStatus,
                                                    Packet *packet)
{
    // Check parameters
    if ((destination == 0) ||
        (clientId == 0) ||
        (clientStatus < ClientStatus_Active) ||
        (clientStatus > ClientStatus_ErrorUnknownClient) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Create Header
    packet->setSource(0u);
    packet->setDestination(destination);
    packet->setPacketType(PacketType_GetClientStatusResponse);
    packet->setPacketId(packetId);

    // Create Payload
    QByteArray payload;

    payload.append(static_cast<char>(clientId));
    payload.append(static_cast<char>(clientStatus));

    packet->setData(payload);

    // Success
    return true;
}

bool DataBus::GetClientStatusResponsePacket::parse(const Packet &packet,
                                                   quint8 *clientId,
                                                   ClientStatus *clientStatus)
{
    // Check parameters
    if ((packet.getSource() != 0) ||
        (packet.getDestination() == 0) ||
        (packet.getPacketType() != PacketType_GetClientStatusResponse) ||
        (clientId == 0) ||
        (clientStatus == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get data
    QByteArray data = packet.getData();

    if (data.size() != 2)
    {
        // Error, invalid size
        return false;
    }

    // Get Client ID
    quint8 id = static_cast<quint8>(data.at(0));

    if (id == 0)
    {
        // Error, invalid Client ID
        return false;
    }

    *clientId = id;

    // Get Client Status
    ClientStatus status = static_cast<ClientStatus>(data.at(1));

    if ((status < ClientStatus_Active) ||
        (status > ClientStatus_ErrorUnknownClient))
    {
        // Error, invalid Client ID
        return false;
    }

    *clientStatus = status;

    // Success
    return true;
}
